package com.have.blog.admin.controller;

import com.have.blog.admin.annotation.operationLogger.OperationLogger;
import com.have.blog.admin.domain.dto.ResourceMenuDto;
import com.have.blog.admin.service.AdminResourceMenuService;
import com.have.blog.base.exception.ThrowableUtils;
import com.have.blog.base.validator.group.Delete;
import com.have.blog.base.validator.group.GetList;
import com.have.blog.base.validator.group.Insert;
import com.have.blog.base.validator.group.Update;
import com.have.blog.base.wrapper.CommonPage;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.core.annotion.avoidRepeatableCommit.AvoidRepeatableCommit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单管理接口
 */
@RestController
@RequestMapping("/categoryMenu")
@Api(value = "菜单信息相关接口", tags = {"菜单信息相关接口"})
@Slf4j
public class AdminCategoryMenuController {
    @Autowired
    private AdminResourceMenuService resourceMenuService;

    @ApiOperation(value = "获取菜单列表", notes = "获取菜单列表")
    @GetMapping(value = "/getList")
    public Wrapper<CommonPage> getList(@Validated({GetList.class}) @RequestBody ResourceMenuDto resourceMenuDto, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        List<ResourceMenuDto> list = resourceMenuService.getList(resourceMenuDto);
        return Wrapper.success(CommonPage.restPage(list));
    }

    @ApiOperation(value = "获取菜单列表", notes = "获取菜单列表")
    @GetMapping(value = "/getAll")
    public Wrapper getAllList(@RequestParam(value = "keyword", required = false) String keyword) {
        List<ResourceMenuDto> allList = resourceMenuService.getHierarchyList();
        return Wrapper.success(allList);
    }

    @ApiOperation(value = "获取所有二级菜单-按钮列表", notes = "获取所有二级菜单-按钮列表")
    @RequestMapping(value = "/getButtonAll", method = RequestMethod.GET)
    public Wrapper getButtonAll(@RequestParam(value = "keyword", required = false) String keyword) {
        List<ResourceMenuDto> allButton = resourceMenuService.getAllButton();
        return Wrapper.success(allButton);
    }

    @AvoidRepeatableCommit
    @OperationLogger(value = "增加菜单")
    @ApiOperation(value = "增加菜单", notes = "增加菜单")
    @PostMapping("/add")
    public Wrapper add(@Validated({Insert.class}) @RequestBody ResourceMenuDto categoryMenuDto, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return resourceMenuService.addResourceMenu(categoryMenuDto);
    }

    @AvoidRepeatableCommit
    @OperationLogger(value = "编辑菜单")
    @ApiOperation(value = "编辑菜单", notes = "编辑菜单")
    @PostMapping("/edit")
    public Wrapper edit(@Validated({Update.class}) @RequestBody ResourceMenuDto categoryMenuDto, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return resourceMenuService.editResourceMenu(categoryMenuDto);
    }

    /**
     * 如果是一级菜单，直接置顶在最前面，二级菜单，就在一级菜单内置顶
     */
    @OperationLogger(value = "置顶菜单")
    @ApiOperation(value = "置顶菜单", notes = "置顶菜单")
    @PostMapping("/stick")
    public Wrapper stick(@Validated({Delete.class}) @RequestBody ResourceMenuDto categoryMenuDto, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return resourceMenuService.stickResourceMenu(categoryMenuDto);
    }

    @OperationLogger(value = "删除菜单")
    @ApiOperation(value = "删除菜单", notes = "删除菜单")
    @PostMapping("/delete")
    public Wrapper delete(@Validated({Delete.class}) @RequestBody ResourceMenuDto categoryMenuDto, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return resourceMenuService.deleteResourceMenu(categoryMenuDto);
    }
}
